﻿using System;
using System.Collections.Generic;

using XStudio.Core.Data;
using XStudio.Core.Log;

using Core.Manage.Product;

namespace Core.Manage.Product
{
    /// <summary>
    /// 生资产品表规则
    /// </summary>
    public partial class GoodsRule : RuleObject<GoodsEntity, GoodsRule>
    {
        #region 新增生资产品表规则
        /// <summary>
        /// 新增生资产品表规则
        /// </summary>
        /// <param name="productionGoodsID"></param>
        /// <param name="goodsProductTypeID"></param>
        /// <param name="productTypeID"></param>
        /// <param name="applyStatus">0 未审核 1 审核通过 2 审核未通过</param>
        /// <param name="name"></param>
        /// <param name="specifications"></param>
        /// <param name="manufactor"></param>
        /// <param name="manufactureDate"></param>
        /// <param name="price"></param>
        /// <param name="unit"></param>
        /// <param name="describe"></param>
        /// <param name="picAddress"></param>
        /// <param name="extField"></param>
        /// <returns>是否新增成功</returns>
        public Result Insert
            (
                int productionGoodsID,
                int goodsProductTypeID,
                int productTypeID,
                int applyStatus,
                string name,
                string specifications,
                string manufactor,
                DateTime manufactureDate,
                Double price,
                string unit,
                string describe,
                string picAddress,
                string extField
            )
        {
            //实体信息
            var curEntity = new GoodsEntity
            {
                //UID= _Db.GetMaxID<GoodsEntity>(),
                ProductionGoodsID = productionGoodsID,
                GoodsProductTypeID = goodsProductTypeID,
                ProductTypeID = productTypeID,
                ApplyStatus = applyStatus,
                Name = name,
                Specifications = specifications,
                Manufactor = manufactor,
                ManufactureDate = manufactureDate,
                Price = price,
                Unit = unit,
                Describe = describe,
                PicAddress = picAddress,
                ExtField = extField,
            };

            //执行操作并返回
            return Insert(curEntity);
        }

        /// <summary>
        /// 新增生资产品表规则
        /// </summary>
        /// <param name="curEntity"></param>
        /// <returns>是否新增成功</returns>
        public new Result Insert(GoodsEntity curEntity)
        {
            // 获取唯一标识
            //if(curEntity.UID == 0)
            //{
            //curEntity.UID = _Db.GetMaxID<GoodsEntity>();
            //}

            // 返回结果对象
            var result = new Result();

            //// 判断唯一性
            //if (GetCount(t => t.Model == curEntity.Model) > 0)
            //{
            //    result.IsOK = false;
            //    result.Description = "已有相同的生资产品表，请检查！";
            //    return result;
            //}

            //执行操作并返回
            try
            {
                result.IsOK = _Db.Insert(curEntity);
                result.StateCodeID = result.IsOK ? curEntity.UID : 0;
            }
            catch (Exception ex)
            {
                result.IsOK = false;
                LoggerHelper.WriteError("添加生资产品表出现异常！", ex);
            }
            result.Description = string.Format("添加生资产品表{0}", (result.IsOK ? "成功。" : "失败！"));
            return result;
        }
        #endregion

        #region 修改生资产品表规则
        /// <summary>
        /// 修改生资产品表规则
        /// </summary>
        /// <param name="goodID"></param>
        /// <param name="productionGoodsID"></param>
        /// <param name="goodsProductTypeID"></param>
        /// <param name="productTypeID"></param>
        /// <param name="applyStatus">0 未审核 1 审核通过 2 审核未通过</param>
        /// <param name="name"></param>
        /// <param name="specifications"></param>
        /// <param name="manufactor"></param>
        /// <param name="manufactureDate"></param>
        /// <param name="price"></param>
        /// <param name="unit"></param>
        /// <param name="describe"></param>
        /// <param name="picAddress"></param>
        /// <param name="extField"></param>
        /// <returns>是否修改成功</returns>
        public Result Update
            (
                int goodID,
                int productionGoodsID,
                int goodsProductTypeID,
                int productTypeID,
                int applyStatus,
                string name,
                string specifications,
                string manufactor,
                DateTime manufactureDate,
                Double price,
                string unit,
                string describe,
                string picAddress,
                string extField
            )
        {
            //实体信息
            var curEntity = new GoodsEntity
            {
                ProductionGoodsID = productionGoodsID,
                GoodsProductTypeID = goodsProductTypeID,
                ProductTypeID = productTypeID,
                ApplyStatus = applyStatus,
                Name = name,
                Specifications = specifications,
                Manufactor = manufactor,
                ManufactureDate = manufactureDate,
                Price = price,
                Unit = unit,
                Describe = describe,
                PicAddress = picAddress,
                ExtField = extField,
            };

            //执行操作并返回
            return Update(curEntity);
        }

        /// <summary>
        /// 修改生资产品表规则
        /// </summary>
        /// <param name="curEntity"></param>
        /// <returns>是否新增成功</returns>
        public new Result Update(GoodsEntity curEntity)
        {
            // 返回结果对象
            var result = new Result();

            //// 判断唯一性
            //if (GetCount(t => t.Model == curEntity.Model && t.UID != curEntity.UID) > 0)
            //{
            //    result.IsOK = false;
            //    result.Description = "已有相同的生资产品表，请检查！";
            //    return result;
            //}

            //执行操作并返回
            try
            {
                result.IsOK = _Db.Update(curEntity, p => p.UID == curEntity.UID);
            }
            catch (Exception ex)
            {
                result.IsOK = false;
                LoggerHelper.WriteError("修改生资产品表出现异常！", ex);
            }
            result.Description = string.Format("修改生资产品表{0}", (result.IsOK ? "成功。" : "失败！"));
            return result;
        }
        #endregion

        #region 删除生资产品表规则
        /// <summary>
        /// 删除生资产品表规则
        /// </summary>
        /// <param name="uid">唯一标识</param>
        /// <returns>是否修改成功</returns>
        public new Result Delete(int uid)
        {
            //执行操作并返回
            var result = new Result();
            result.IsOK = _Db.Delete<GoodsEntity>(p => p.UID == uid);
            result.Description = string.Format("删除生资产品表{0}", (result.IsOK ? "成功。" : "失败！"));
            return result;
        }
        #endregion
    }
}
